package vn.njai.thousand_english_word.CustomUI;

import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v4.widget.DrawerLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import butterknife.ButterKnife;
import butterknife.InjectView;
import com.gc.materialdesign.widgets.Dialog;
import vn.njai.thousand_english_word.Activity.CheckActivity;
import vn.njai.thousand_english_word.Activity.HistoryActivity;
import vn.njai.thousand_english_word.Activity.ProgressActivity;
import vn.njai.thousand_english_word.Activity.SettingActivity;
import vn.njai.thousand_english_word.ChatHead.ChatHeadService;
import vn.njai.thousand_english_word.R;
import vn.njai.thousand_english_word.UIEntities.CustomMenuItem;

import java.util.ArrayList;

/**
 * Created by Hautu on 12/4/2014.
 */
public class CustomMenuView extends LinearLayout {
    private String TAG = "CustomMenuView";
    private Context mContext;
    private Adapter mAdapter;
    private DrawerLayout mDrawerLayout;

    @InjectView(R.id.listMenu) public ListView mMenuList;

    public CustomMenuView(Context context) {
        this(context,null);
    }

    public CustomMenuView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CustomMenuView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    public void setDrawerLayout(DrawerLayout drawerLayout){
        mDrawerLayout = drawerLayout;
    }

    private void init() {
        View view = inflate(getContext(), R.layout.custom_menu, null);
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        addView(view);
        ButterKnife.inject(this);

        mAdapter = new Adapter();
        mMenuList.setAdapter(mAdapter);
        mMenuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        HistoryActivity.start(mContext);
                        break;
                    case 1:
                        CheckActivity.start(mContext);
                        break;
                    case 2:
                        ProgressActivity.start(mContext);
                        break;
                    case 3:
                        SettingActivity.start(mContext);
                        break;
                    case 4:
                        Dialog dialog = new Dialog(mContext,mContext.getString(R.string.dialog_exit_title), mContext.getString(R.string.dialog_exit_question));
                        dialog.show();
                        Typeface face=Typeface.createFromAsset(mContext.getAssets(),mContext.getString(R.string.font_name));
                        dialog.getButtonAccept().getTextView().setTypeface(face);
                        dialog.getButtonCancel().getTextView().setTypeface(face);
                        dialog.getButtonAccept().setText(mContext.getString(R.string.dialog_exit_ok));
                        dialog.getButtonCancel().setText(mContext.getString(R.string.dialog_exit_cancel));
                        dialog.getButtonAccept().setRippleSpeed(7);
                        dialog.getButtonCancel().setRippleSpeed(7);
                        dialog.setOnAcceptButtonClickListener(new OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                mContext.stopService(new Intent(mContext,ChatHeadService.class));
                                System.exit(1);
                            }
                        });
                        break;
                }
               mDrawerLayout.closeDrawers();
            }
        });
        initMenuItem();
    }

    private void initMenuItem(){
        ArrayList<CustomMenuItem> mMenuItems = new ArrayList<CustomMenuItem>();
        mMenuItems.add(new CustomMenuItem(R.drawable.ic_menu_history,mContext.getString(R.string.menu_title_history),0));
        mMenuItems.add(new CustomMenuItem(R.drawable.ic_menu_random,mContext.getString(R.string.menu_title_check),0));
        mMenuItems.add(new CustomMenuItem(R.drawable.ic_menu_progress,mContext.getString(R.string.menu_title_progress),0));
        mMenuItems.add(new CustomMenuItem(R.drawable.ic_menu_setting,mContext.getString(R.string.menu_title_setting),0));
        mMenuItems.add(new CustomMenuItem(R.drawable.ic_menu_exit,mContext.getString(R.string.menu_title_exit),0));
        mAdapter.addAll(mMenuItems);
    }

    public class Adapter extends ArrayAdapter<CustomMenuItem> {

        public class ViewHolder{
            @InjectView(R.id.txtName) public TextView mNameView;
            @InjectView(R.id.imgIcon) public ImageView mIconView;

            public ViewHolder(View view){
                ButterKnife.inject(this,view);
            }
        }

        public Adapter() {
            super(mContext, 0, new ArrayList<CustomMenuItem>());
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view  = convertView;
            CustomMenuItem item = getItem(position);
            ViewHolder viewHolder;
            if (view == null) {
                view = LayoutInflater.from(getContext()).inflate(R.layout.custom_item_menu, parent, false);
                viewHolder = new ViewHolder(view);
                view.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder)view.getTag();
            }
            viewHolder.mNameView.setText(item.mName);
            viewHolder.mIconView.setImageDrawable(getResources().getDrawable(item.mUiId));
            return view;
        }
    }
}
